<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}配送员`">
    <el-descriptions :column="1" title="配送员信息" border>
      <el-descriptions-item width="300" label="配送员账号">{{ drawerProps.row!.username }}</el-descriptions-item>
      <el-descriptions-item width="300" label="配送员状态">
        <el-tag v-if="drawerProps.row.userStatus === 0" type="danger">禁用</el-tag>
        <el-tag v-if="drawerProps.row.userStatus === 1" type="success">已启用</el-tag>
      </el-descriptions-item>
      <el-descriptions-item width="300" label="所在分站">{{ drawerProps.row!.departmentName }}</el-descriptions-item>
      <el-descriptions-item width="300" label="角色">
        <el-tag v-if="drawerProps.row.userRole === 8">配送员</el-tag>
      </el-descriptions-item>
      <el-descriptions-item width="300" label="创建时间">{{ drawerProps.row!.createTime }}</el-descriptions-item>
    </el-descriptions>
    <p></p>
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="配送员头像" prop="userAvatar">
        <UploadImg v-model:image-url="drawerProps.row!.userAvatar" width="135px" height="135px" :file-size="3">
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传头像</span>
          </template>
          <template #tip> 头像大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="头像选择">
        <el-button @click="changeAvatar" type="primary">随机头像</el-button>
      </el-form-item>
      <p></p>
      <el-form-item label="配送员姓名" prop="name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写配送员姓名" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="userGender">
        <el-select v-model="drawerProps.row!.userGender" placeholder="请选择性别" clearable>
          <el-option v-for="item in genderType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="身份证号" prop="userIdentity">
        <el-input v-model="drawerProps.row!.userIdentity" placeholder="请填写身份证号" clearable></el-input>
      </el-form-item>
      <el-form-item label="电话号" prop="userPhone">
        <el-input v-model="drawerProps.row!.userPhone" placeholder="请填写电话号" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { genderType } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { User } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import { imageLinks } from "@/utils/avatarLinks";

const rules = reactive({
  name: [
    { required: true, message: "请填写用户姓名" },
    { pattern: /^[\u4e00-\u9fa5]{2,4}$/, message: "姓名格式不正确" }
  ],
  userGender: [{ required: true, message: "请选择性别" }],
  username: [
    { required: true, message: "请填写用户账号" },
    { min: 4, max: 20, message: "账号长度为4到20个字符" }
  ],
  userIdentity: [
    { required: true, message: "请填写身份证号" },
    { pattern: /^\d{17}[\dXx]$/, message: "身份证号格式不正确" }
  ],
  userPhone: [
    { required: true, message: "请填写电话号" },
    { pattern: /^1[3-9]\d{9}$/, message: "电话号格式不正确" }
  ]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      const editUserParams = ref<Partial<User.EditUserParams>>({
        userId: drawerProps.value.row.userId,
        name: drawerProps.value.row.name,
        userAvatar: drawerProps.value.row.userAvatar,
        userIdentity: drawerProps.value.row.userIdentity,
        userGender: drawerProps.value.row.userGender,
        userPhone: drawerProps.value.row.userPhone
      });
      await drawerProps.value.api!(editUserParams.value);
      ElMessage.success({ message: `${drawerProps.value.title}配送员成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

const changeAvatar = () => {
  //随机选取一个头像
  drawerProps.value.row.userAvatar = imageLinks[Math.floor(Math.random() * imageLinks.length)];
};

defineExpose({
  acceptParams
});
</script>
